package com.qsxc.dao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qsxc.business.bean.R;
import com.qsxc.business.promoter.bean.req.CashApplyReq;
import com.qsxc.business.promoter.bean.vo.CashRecordVo;
import com.qsxc.business.promoter.bean.vo.DivideRecordVo;
import com.qsxc.dao.entity.SmPromoter;
import com.qsxc.dao.entity.SmPromoterCashDivideRecord;
import com.qsxc.dao.entity.SmPromoterCashRecord;
import com.qsxc.dao.entity.SmPromoterDivideRecord;
import com.qsxc.dao.mapper.SmPromoterCashDivideRecordMapper;
import com.qsxc.dao.mapper.SmPromoterCashRecordMapper;
import com.qsxc.dao.mapper.SmPromoterDivideRecordMapper;
import com.qsxc.dao.mapper.SmPromoterMapper;
import com.qsxc.dao.service.ISmPromoterCashRecordService;
import com.qsxc.utils.CollectionUtil;
import com.qsxc.utils.validator.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 推广员提现记录表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-25
 */
@Slf4j
@Service
public class SmPromoterCashRecordServiceImpl extends ServiceImpl<SmPromoterCashRecordMapper, SmPromoterCashRecord> implements ISmPromoterCashRecordService {
    @Resource
    private SmPromoterMapper smPromoterMapper;
    @Resource
    private SmPromoterCashRecordMapper smPromoterCashRecordMapper;
    @Resource
    private SmPromoterDivideRecordMapper smPromoterDivideRecordMapper;
    @Resource
    private SmPromoterCashDivideRecordMapper smPromoterCashDivideRecordMapper;

    @Override
    public R<List<CashRecordVo>> getPromoterCashRecord(String openid, String busiStatus) throws Exception {
        // 根据openid查询推广员信息
        SmPromoter smPromoter = smPromoterMapper.queryByOpenid(openid);
        Assert.isNull(smPromoter, "推广员信息不存在");

        Long promoterId = smPromoter.getId();
        // 根据推广员id查询提现记录列表
        List<CashRecordVo> cashRecordVoList = smPromoterCashRecordMapper.queryListByPromoterId(promoterId, busiStatus);
        if (CollectionUtil.isEmpty(cashRecordVoList)) {
            return R.success();
        }

        for (CashRecordVo cashRecordVo : cashRecordVoList) {
            Long cashRecordId = cashRecordVo.getId();

            // 根据提现记录id查询分成记录列表
            List<DivideRecordVo> divideRecordVoList = smPromoterDivideRecordMapper.queryListByCashRecordId(cashRecordId);
            if (CollectionUtil.isNotEmpty(divideRecordVoList)) {
                cashRecordVo.setDivideRecords(divideRecordVoList);
            }
        }

        return R.success(cashRecordVoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveCashApply(CashApplyReq cashApplyReq) throws Exception {
        String openid = cashApplyReq.getOpenid();
        // 根据openid查询推广员信息
        SmPromoter smPromoter = smPromoterMapper.queryByOpenid(openid);
        Assert.isNull(smPromoter, "推广员信息不存在");

        Long promoterId = smPromoter.getId();
        String divideIds = cashApplyReq.getDivideIds();
        String[] idArray = divideIds.split(",");
        // 根据推广员ID、记录ID查询分账记录列表
        List<SmPromoterDivideRecord> smPromoterDivideRecordList = smPromoterDivideRecordMapper.queryListByPromoterIdAndIds(promoterId, idArray);
        if (CollectionUtil.isEmpty(smPromoterDivideRecordList)) {
            return R.fail("没有可提现的分账记录");
        }

        // 判断分成记录数量与divideIds数量是否一致
        if (smPromoterDivideRecordList.size() != idArray.length) {
            return R.fail("可提现分账记录与选择的记录不一致");
        }

        // 对smPromoterDivideRecordList中分成记录amountMoney求和
        Double totalAmountMoney = smPromoterDivideRecordList.stream().mapToDouble(SmPromoterDivideRecord::getAmountMoney).sum();
        // 保存提现申请记录SmPromoterCashRecord表
        SmPromoterCashRecord smPromoterCashRecord = new SmPromoterCashRecord();
        smPromoterCashRecord.setCreateBy(promoterId);
        smPromoterCashRecord.setCreateTime(new Date());
        smPromoterCashRecord.setPromoterId(promoterId);
        smPromoterCashRecord.setAmountMoney(totalAmountMoney);
        smPromoterCashRecord.setBusiStatus("1");
        smPromoterCashRecord.setStatus("1");
        smPromoterCashRecordMapper.insert(smPromoterCashRecord);

        // 保存提现申请记录、分成记录关系记录SmPromoterCashDivideRecord表
        SmPromoterCashDivideRecord smPromoterCashDivideRecord = null;
        for (SmPromoterDivideRecord smPromoterDivideRecord : smPromoterDivideRecordList) {
            smPromoterCashDivideRecord = new SmPromoterCashDivideRecord();
            smPromoterCashDivideRecord.setCreateBy(promoterId);
            smPromoterCashDivideRecord.setCreateTime(new Date());
            smPromoterCashDivideRecord.setPromoterId(promoterId);
            smPromoterCashDivideRecord.setCashId(smPromoterCashRecord.getId());
            smPromoterCashDivideRecord.setDivideId(smPromoterDivideRecord.getId());
            smPromoterCashDivideRecordMapper.insert(smPromoterCashDivideRecord);
        }

        // 将提现申请记录ID更新到分成记录表
        smPromoterDivideRecordList.forEach(smPromoterDivideRecord -> {
            smPromoterDivideRecord.setCashRecordId(smPromoterCashRecord.getId());
            smPromoterDivideRecordMapper.updateById(smPromoterDivideRecord);
        });

        return R.success();
    }
}
